package com.yixin.ystartlibrary.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;

import static android.view.View.GONE;

/**
* Content:动画工具类
* Created By WDY
* Date: 2017/7/13 14:25
*/

public class AnimationUtils {

    //沿x轴放大
//    ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
    //沿y轴放大
//    ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f, 1f);
    //旋转
//    ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f, 0f);
    //移动
//    ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f, 0f);
    //透明动画
//    ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
//    animator.setDuration(2000);
//    animator.start();
//    AnimatorSet set = new AnimatorSet();
    //同时沿X,Y轴放大，且改变透明度，然后移动
//    set.play(scaleXAnimator).with(scaleYAnimator).with(animator).before(translationXAnimator);
   //都设置3s，也可以为每个单独设置
//    set.setDuration(3000);
//    set.start();
    //添加监听事件
//        set.addListener(new Animator.AnimatorListener() {
//        @Override
//        public void onAnimationStart(Animator animation) {
            //动画开始的时候调用
//        }
//
//        @Override
//        public void onAnimationEnd(Animator animation) {
           //画结束的时候调用
//        }
//
//        @Override
//        public void onAnimationCancel(Animator animation) {
           //动画被取消的时候调用
//        }
//
//        @Override
//        public void onAnimationRepeat(Animator animation) {
            //动画重复执行的时候调用
//
//        }
//    });
    //另一种设置监听的方式，里面的监听方法可以选择性重写
//    set.addListener(new AnimatorListenerAdapter() {
//        @Override
//        public void onAnimationStart(Animator animation) {
//
//        }
//    });

    public static void appear(final View v, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(v, "alpha", 0f, 1f).setDuration(duration);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                v.setVisibility(View.VISIBLE);
            }
        });
        animator.start();
    }

    public static void disappear(final View v, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(v, "alpha", 1f, 0f).setDuration(duration);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                v.setVisibility(GONE);
            }
        });
        animator.start();
    }
}
